package com.mdd.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.common.entity.Label;
import com.mdd.common.entity.LabelIndex;
import com.mdd.common.mapper.LabelIndexMapper;
import com.mdd.common.mapper.LabelMapper;
import com.mdd.common.service.ILabelIndexService;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.validate.LabelIndexSearchValidate;
import com.mdd.common.validate.LabelVoValidate;
import com.mdd.common.vo.LabelDataVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 标签关联服务
 */
@Service
public class LabelIndexServiceImpl implements ILabelIndexService {

    @Resource
    private LabelMapper labelMapper;

    @Resource
    LabelIndexMapper labelIndexMapper;

    @Override
    public List<LabelDataVo> all() {
        List<Label> list = labelMapper.selectList(new QueryWrapper<Label>().eq("is_delete", 0));
        List<LabelDataVo> result = new ArrayList<>();
        list.forEach(item -> {
            LabelDataVo vo = new LabelDataVo();
            vo.setValue(item.getId());
            vo.setLabel(item.getName());
            vo.setCode(item.getValue());
            vo.setColor(item.getColor());
            vo.setBackgroundColor(item.getBackgroundColor());
            result.add(vo);
        });
        return result;
    }

    @Override
    public void addIndexLabel(LabelVoValidate labelVoValidate) {
        String sourceType = labelVoValidate.getSourceType();
        List<Integer> labelList = labelVoValidate.getLabelList();

        // 查询
        LambdaQueryWrapper<LabelIndex> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabelIndex::getSourceId, labelVoValidate.getSourceId());
        wrapper.eq(LabelIndex::getSourceType, sourceType);
        // 更新
        List<LabelIndex> list = labelIndexMapper.selectList(wrapper);
        list.stream().forEach(item -> {
            item.setIsShow(0);
            labelIndexMapper.updateById(item);
        });
        // 插入 更新
        labelList.stream().forEach(labelId -> {
            wrapper.clone();

            wrapper.eq(LabelIndex::getLabelId, labelId);
            wrapper.last("limit 1");
            LabelIndex labelIndex = labelIndexMapper.selectOne(wrapper);
            if (Objects.nonNull(labelIndex)) {
                labelIndex.setIsShow(1);
                labelIndex.setUpdateTime(TimeUtils.timestamp());
                labelIndexMapper.updateById(labelIndex);
            } else {
                labelIndex = new LabelIndex();
                labelIndex.setLabelId(labelId);
                labelIndex.setSourceId(labelVoValidate.getSourceId());
                labelIndex.setSourceType(sourceType);
                labelIndex.setCreateTime(TimeUtils.timestamp());
                labelIndex.setUpdateTime(TimeUtils.timestamp());
                labelIndexMapper.insert(labelIndex);
            }
        });
    }

    @Override
    public List<Integer> getLabelIds(LabelIndexSearchValidate searchValidate) {
        // 查询
        LambdaQueryWrapper<LabelIndex> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabelIndex::getSourceId, searchValidate.getSourceId());
        wrapper.eq(LabelIndex::getSourceType, searchValidate.getSourceType());
        wrapper.eq(LabelIndex::getIsShow, 1).eq(LabelIndex::getIsDelete, 0);
        List<LabelIndex> list = labelIndexMapper.selectList(wrapper);
        List<Integer> labelList = list.stream().map(item -> item.getLabelId()).collect(Collectors.toList());

        return labelList;
    }

    @Override
    public List<LabelDataVo> getLabelList(LabelIndexSearchValidate searchValidate) {
        LambdaQueryWrapper<LabelIndex> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabelIndex::getSourceId, searchValidate.getSourceId());
        wrapper.eq(LabelIndex::getSourceType, searchValidate.getSourceType());
        wrapper.eq(LabelIndex::getIsShow, 1).eq(LabelIndex::getIsDelete, 0);
        List<LabelIndex> list = labelIndexMapper.selectList(wrapper);
        List<Integer> labelList = list.stream().map(item -> item.getLabelId()).collect(Collectors.toList());
        List<LabelDataVo> result = new ArrayList<>();
        if (Objects.nonNull(labelList) && labelList.size() > 0) {
            List<Label> labels = labelMapper.selectList(new QueryWrapper<Label>().in("id", labelList));
            labels.stream().forEach(item -> {
                LabelDataVo vo = new LabelDataVo();
                BeanUtils.copyProperties(item, vo);
                vo.setValue(item.getId());
                vo.setLabel(item.getName());
                result.add(vo);
            });
        }
        return result;
    }

    @Override
    public LabelDataVo getLabelInfo(String labelCode) {

        Label label = labelMapper.selectOne(new LambdaQueryWrapper<Label>().eq(Label::getValue, labelCode).last("limit 1"));

        LabelDataVo vo = new LabelDataVo();
        if (Objects.nonNull(label)) {
            vo.setValue(label.getId());
            vo.setCode(label.getValue());
            vo.setColor(label.getColor());
            vo.setBackgroundColor(label.getBackgroundColor());
        }
        return vo;
    }
}
